<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <#include "../common/header.ftl"/>
    <script type="text/javascript" src="/js/plugins/ckeditor/ckeditor/ckeditor.js"></script>

    <script>
        $(function () {
           var ck= CKEDITOR.replace('editor', {
                //图片预览服务器:存放之前传过图片
                filebrowserBrowseUrl: '/kkk',
                //图片上传请求路径
                filebrowserUploadUrl: '/uploadImages.do'
            });


            //二级联动,绑定一个值改变事件
            $("#strategy").change(function () {
                var strategyId = $(this).val();
                var html = "";
                $.get("/strategyCatalog/getStrategyCatalogByStrategyId.do", {strategyId: strategyId}, function (data) {
                    $.each(data, function (index, ele) {
                        html += "<option value=" + ele.id + ">" + ele.name + "</option>"
                    })
                    $("#strategyCatalog").html(html)

                })
            })


            $(".changeBtn").click(function () {
                //清除模态框中的表单
                $("#editForm").clearForm(true);

                var json = $(this).data("json")
                $.get("/strategyDetail/getStrategyByStrategyDetailId.do", {strategyDetailId: json.id}, function (data) {
                    var html = "";
                    $("#strategy").val(data.id)
                    $.get("/strategyCatalog/getStrategyCatalogByStrategyId.do", {strategyId: data.id}, function (data) {
                        $.each(data, function (index, ele) {
                            html += "<option value=" + ele.id + ">" + ele.name + "</option>"
                        })
                        $("#strategyCatalog").html(html)
                        $("#editForm input[name='id']").val(json.id)
                        $("#editForm input[name='title']").val(json.title)
                        $("#editForm select[name='strategyCatalog.id']").val(json.strategyCatalogId)
                        $("#editForm select[name='state']").val(json.state)
                        $("#editForm input[name='sequence']").val(json.sequence)
                        $("#editForm input[name='coverUrl']").val(json.coverUrl)
                        //因为在strategyDetail中,state是boolean类型,所以要加+""
                        $("#coverUrl").prop("src", json.coverUrl)


                    })
                    //文章内容回显
                    $.get("/strategyDetail/getContentByStrategyDetailId.do",{detailId:json.id},function (data) {
                        $("#editor").val(data.content)
                        //必须设置到CKeditor中才能显示
                        ck.setData(data.content)
                    })

                })


                $("#inputModal").modal('show')
            })

            //新增事件
            $("#inputBtn").click(function () {
                $("#editForm").clearForm(true);
                $("#coverUrl").prop("src", "")
                $("#inputModal").modal('show')
            })

            $("#saveBtn").click(function () {
                var value = ck.getData();
                $("#editor").val(value);

                $("#editForm").ajaxSubmit(function (data) {
                    if (data.success) {
                        window.location.reload()
                    }
                })
            })

        })
    </script>


</head>
<body>

<div class="container " style="margin-top: 20px">
    <#include "../common/top.ftl"/>
    <div class="row">
        <div class="col-sm-3">
        <#--设置当前要回显当前菜单,必须在载人菜单前完成设置-->
            <#assign menu="strategyDetail"/>
            <#include "../common/menu.ftl"/>
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">攻略文章管理</h1>
                </div>
            </div>
            <!--高级查询--->
            <form class="form-inline" id="searchForm" action="" method="post">
                <input type="hidden" id="currentPage" name="currentPage" value="1">
                <div class="form-group">
                    <label class="control-label">攻略搜索</label>
                    <div class="form-group">
                        <label>关键字:</label>
                        <input type="text" class="form-control" name="keyword" value="${(qo.keyword)!}"
                               placeholder="请输入标题">
                    </div>
                </div>
                <button id="btn_query" class="btn btn-info">
                    <span class="glyphicon glyphicon-search"></span> 查询
                </button>
                <button id="inputBtn" type="button" class="btn btn-success">
                    <span class="glyphicon glyphicon-plus"></span> 添加攻略文章
                </button>
            </form>

            <table class="table table-striped table-hover">
                <tr>
                    <th>标题</th>
                    <th>封面</th>
                    <th>发布时间</th>
                    <th>排序</th>
                    <th>攻略类别</th>
                    <th>状态</th>
                    <th></th>
                </tr>
                <#list pageInfo.list as entity>
                    <tr>
                        <td>${entity.title!}</td>
                        <td><img src="${entity.coverUrl!}" width="30px"></td>
                        <td>${(entity.releaseTime?string("yyyy-MM-dd HH:mm:ss"))!}</td>
                        <td>${entity.sequence!}</td>
                        <td>${entity.strategyCatalog.name!}</td>
                        <td>${entity.stateName!}</td>
                        <td>
                            <a href="javaScript:;" class="changeBtn" data-json='${(entity.jsonString)!}'>修改</a>
                        </td>
                    </tr>
                </#list>
            </table>
           <#include "../common/page.ftl"/>
        </div>
    </div>
</div>

<#--模态框-->
<div id="inputModal" class="modal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">编辑/增加</h4>
            </div>
            <div class="modal-body">
                <form id="editForm" class="form-horizontal" method="post" action="/strategyDetail/save.do"
                      enctype="multipart/form-data" style="margin: -3px 118px">
                    <input type="hidden" name="id"/>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">标题</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="title" placeholder="分类名称">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">封面</label>
                        <div class="col-sm-6">
                            <img id="coverUrl" width="200px"/>
                            <input type="hidden" name="coverUrl" id="cover">
                            <input type="file" class="form-control" id="pic" name="pic">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">所属攻略</label>
                        <div class="col-sm-6">
                            <select id="strategy" name="strategyCatalog.strategy.id" class="form-control">
                                <#list Strategys as s >
                                    <option value="${s.id}">${s.title}</option>
                                </#list>
                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-4 control-label">攻略类别</label>
                        <div class="col-sm-6">
                            <select id="strategyCatalog" name="strategyCatalog.id" class="form-control">
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">序号</label>
                        <div class="col-sm-6">
                            <input id="sequence" name="sequence" class="form-control"/>
                            </input>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-4 control-label">是否发布</label>
                        <div class="col-sm-6">
                            <select id="state" class="form-control" autocomplete="off" name="state">
                                <option value="0">是</option>
                                <option value="1">否</option>
                            </select>
                        </div>
                    </div>
                    <textarea id="editor" name="strategyContent.content"></textarea>
                </form>
            </div>
            <div class="modal-footer">
                <a href="javascript:void(0);" class="btn btn-success" id="saveBtn" aria-hidden="true">保存</a>
                <a href="javascript:void(0);" class="btn" data-dismiss="modal" aria-hidden="true">关闭</a>
            </div>
        </div>
    </div>
</div>

</body>
</html>